// userPageC/pages/confirmOrder/confirmOrder.js
const app = getApp()
const WXAPI = require("../../../api/api") 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1,
      // 是否显示左上角图标   1表示显示    0表示不显示
      title: '确认订单',
      // 导航栏 中间的标题
      textColor: '#222',
      // 标题颜色
      bgColor: '#fff',
      // 导航栏背景颜色
      isloading: true,
      isColor: true
    },
    imgHttp: app.globalData.imgHttp,
    grouponId: '',
    orderData: {},
    payData: {},
    orderId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var grouponId = options.grouponId
    this.setData({
      grouponId:grouponId
    })
    console.log('grouponId',this.data.grouponId)
    this.getOrder()
  },
  getOrder(){
    var grouponId = this.data.grouponId
    var url = `/app/v1/customer/groupon/order/confirmOrder/${grouponId}`
    WXAPI.confirmOrderTuan(url,'',res => {
      if(res.code == 200){
        this.setData({
          orderData: res.result
        })
      }
    })
  },
  //确认支付
  handlePay(){
    var data = {
      groupId: this.data.orderData.groupId,
      buyMoney: this.data.orderData.buyMoney,
      paySource: app.globalData.paySource,
      buyCount: this.data.orderData.buyCount,
    }
    WXAPI.createGrouponOrder(data,res => {
      const data = res.result
      if(res.code == 200){
        const payData = JSON.parse(data)
        console.log('payData',payData)
        this.setData({
          payData: payData
        })
        this.pay()
      }
    })
  },
  pay(){
    const that = this
    const payData = this.data.payData
    console.log('payData',payData)
    wx.requestPayment({
      'timeStamp': payData.timeStamp,
      'nonceStr': payData.nonceStr,
      'package': payData.package,
      'signType': payData.signType, //MD5
      'paySign': payData.paySign,
      'success': function (res) {
        console.log('支付成功', res)
        console.log(res)
        if (res.errMsg == 'requestPayment:ok') {
          wx.showToast({
            title: '支付成功',
          })
          that.setData({
            orderId: payData.orderId
          })
          that.getCardPayorder()
        }
      },
      'fail': function (res) {
        wx.showToast({
          title: '支付失败',
          icon: 'none'
        })
        console.log('支付失败或取消支付', res)
      },
      'complete': function (res) {
        console.log('支付完成', res)
        // wx.navigateBack({})
      }
    })
  },
  //购卡订单支付结果
  getCardPayorder() {
    const that = this
    this.setData({
      timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
        that.data.countdown--;
        var data = {
          orderId: that.data.orderId,
        }
        WXAPI.getBuyCardPayResult(data, (res) => {
          const data = res.result
          console.log("获取支付结果", res)
          if (data.isPay) {
            // wx.redirectTo({
            //   url: '/userPageB/pages/userStore/resellScuuess/resellScuuess?orderId=' + that.data.orderId + "&pathType=cardPay",
            // })
            wx.reLaunch({
              url: '/userPageB/pages/buyCardpuch/buyCardpuch?customerCardId=' + data.customerCardId+'&payType=tuangou',
            })
            clearInterval(that.data.timer);
          }
        })
        that.setData({
          countdown: that.data.countdown,
        })
        if (that.data.countdown == 0) {
          console.log("1111111111")
          clearInterval(that.data.timer);
          that.setData({
            countdown: 60,
          })
        }
      }, 1000)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})